﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;
using SERP.StringMappings;
using SERP.DAO;
using SERP.Controllers;
namespace SERP.Modules.Users{
	public partial class UsersList : SERP.Modules.ModuleBase
	{
		private UsersController controller = new UsersController();    
		protected void Page_Load(object sender, EventArgs e){
			LoadLanguage();
			if (!this.IsPostBack)
			{			
				LoadData();
			}     
		}
		private int GetPageIndex(){
			int pageIndex = 0;
			if (Request["PageIndex"] != null)
			{
				try{
					pageIndex = int.Parse(Request["PageIndex"]);
				}
				catch{
				}
			}
			return pageIndex;
		}
		private string GetFilter(){
			string strFilter="";
			if(txtUserName.Text.Trim()!=""){
					strFilter+=" AND UserName LIKE '%"+txtUserName.Text.Trim()+"%'";
			}			
			if(txtPassword.Text.Trim()!=""){
					strFilter+=" AND Password LIKE '%"+txtPassword.Text.Trim()+"%'";
			}			
			if(txtDisplayName.Text.Trim()!=""){
					strFilter+=" AND DisplayName LIKE '%"+txtDisplayName.Text.Trim()+"%'";
			}			
			if(cbkFindBirthDay.Checked){
				strFilter+=" AND BirthDay BETWEEN "+txtBirthDayFROM.SelectedDate.Value.ToString()+ " AND "+txtBirthDayTO.SelectedDate.Value.ToString();
			}			
			if(txtEmail.Text.Trim()!=""){
					strFilter+=" AND Email LIKE '%"+txtEmail.Text.Trim()+"%'";
			}			
			if(txtGender.Text.Trim()!=""){
				strFilter+=" AND Gender="+txtGender.Text.Trim();
			}			
			if(txtAddress.Text.Trim()!=""){
					strFilter+=" AND Address LIKE '%"+txtAddress.Text.Trim()+"%'";
			}			
			if(txtPhone.Text.Trim()!=""){
					strFilter+=" AND Phone LIKE '%"+txtPhone.Text.Trim()+"%'";
			}			
			if(txtIsActived.SelectedIndex>0){
				strFilter+=" AND IsActived="+(txtIsActived.SelectedIndex==1?"1":"0");
			}
			if(txtUserType.Text.Trim()!=""){
				strFilter+=" AND UserType="+txtUserType.Text.Trim();
			}			
			if(strFilter.Length>0)
				strFilter=strFilter.Substring(4);
			return strFilter;
		}
		//Load All lable text
		private void LoadLanguage(){	
			labelUserName.Text=UsersStringMapping.UserName+":";				
			labelPassword.Text=UsersStringMapping.Password+":";				
			labelDisplayName.Text=UsersStringMapping.DisplayName+":";				
			labelBirthDay.Text=UsersStringMapping.BirthDay+":";				
			cbkFindBirthDay.Text=SERP.Constants.APP_SEARCH;
			labelEmail.Text=UsersStringMapping.Email+":";				
			labelGender.Text=UsersStringMapping.Gender+":";				
			validExpressionGender.ErrorMessage=SERP.Constants.APP_REQUIRE_INTERGER_WARNING;			
			labelAddress.Text=UsersStringMapping.Address+":";				
			labelPhone.Text=UsersStringMapping.Phone+":";				
			labelIsActived.Text=UsersStringMapping.IsActived+":";				
			if(!this.IsPostBack){
				txtIsActived.Items.Add(SERP.Constants.APP_ALL);			
				txtIsActived.Items.Add(SERP.Constants.APP_TRUE);		
				txtIsActived.Items.Add(SERP.Constants.APP_FALSE);		
			}
			labelUserType.Text=UsersStringMapping.UserType+":";				
			validExpressionUserType.ErrorMessage=SERP.Constants.APP_REQUIRE_INTERGER_WARNING;			
			btnSearch.Text=SERP.Constants.APP_SEARCH;
			btnAddNew.Text=SERP.Constants.APP_ADDNEW;
			btnDelete.Text=SERP.Constants.APP_DELETE;
			btnHelps.Text=SERP.Constants.APP_HELPS;
            btnDelete.OnClientClick = "return confirm(\"" + Constants.APP_CONFIRM_DELELE + "\")";
			lblTitle.InnerText=SERP.Constants.APP_LIST+" "+UsersStringMapping.Users;
		}
		private void LoadData(bool clearCache){
			//Load data to grid
			string filter=GetFilter();
			controller.LoadAllDSUsers(clearCache);
			controller.Table.DefaultView.RowFilter=filter;
			gridData.DataSource=controller.Table;
			//gridData.PageIndex=GetPageIndex();
			gridData.DataBind();
		}
		private void LoadData()
		{
			LoadData(true);
		}
		private Dictionary<string, string> GetParameter(string stParam)
        {
            Dictionary<string, string> dic = new Dictionary<string, string>();
            foreach (string st in stParam.Split(','))
            {
                string[] arr = st.Split('=');
                dic.Add(arr[0], arr[1]);
            }
            return dic;
        }
		private void DeleteData(){
			for(int indexRow=0;indexRow<gridData.Items.Count;indexRow++){
				RepeaterItem row = gridData.Items[indexRow];
				CheckBox cbkRow=(CheckBox)row.FindControl("cbkUsers");
				if(cbkRow!=null){
					if(cbkRow.Checked){
						LinkButton lkDelete=(LinkButton)row.FindControl("btnXoa");
                        Dictionary<string, string> dic = GetParameter(lkDelete.CommandArgument.ToString().Substring(1));
						controller.DeleteByID(Int32.Parse(dic["UserId"]));
					}
				}
			}
		}
		protected void btnSearch_Click(object sender, EventArgs e)
		{
			LoadData(false);
		}
		protected void OnAddNew(object sender, EventArgs e)
		{
			RedirectToPage("UsersAddNew");
		}
		protected void OnDelete(object sender, EventArgs e)
		{
			DeleteData();	
			LoadData();
		}
		protected void btnXem_OnCommand(object sender,CommandEventArgs e){
			RedirectToPage("","","UsersView",e.CommandArgument.ToString().Trim().TrimStart('&'));
		}
		protected  void btnSua_OnCommand(object sender, CommandEventArgs e)
		{
			RedirectToPage("", "", "UsersEdit", e.CommandArgument.ToString().Trim().TrimStart('&'));
		}
		protected  void btnXoa_OnCommand(object sender, CommandEventArgs e)
		{
			RedirectToPage("", "", "UsersDelete", e.CommandArgument.ToString().Trim().TrimStart('&'));
		}
	}
}
